/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package econutri.servicos.recomendacao;

import econutri.servicos.recordatorio.TempRecordatorio;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lomtools.persistence.GenericDAO;

/**
 *
 * @author leoomoreira
 */
public class ListarRecomendacaoXMLServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("application/xml;charset=ISO-8859-1");
        String xml = "";
        String pacienteCodigo = request.getParameter("pacienteCodigo");
        String sql = "SELECT al.codigo, al.descricao, a.refeicaocodigo, r.descricao as refeicaodescricao, a.tipocodigo FROM recomendacao a, alimento al, refeicao r WHERE a.pacientecodigo = " + pacienteCodigo + " AND a.alimentocodigo = al.codigo AND a.refeicaocodigo = r.codigo ORDER BY a.tipocodigo, a.refeicaocodigo";
        try {
            GenericDAO dao = new GenericDAO();
            ResultSet rs = dao.executeQuery(sql);
            List<TempRecomendacao> resultList = new ArrayList<TempRecomendacao>();
            while (rs != null && rs.next()) {
                TempRecomendacao tempRecomendacao = new TempRecomendacao();
                tempRecomendacao.setAlimentoCodigo(rs.getInt("codigo"));
                tempRecomendacao.setAlimentoDescricao(rs.getString("descricao"));
                tempRecomendacao.setTipoCodigo(rs.getInt("tipocodigo"));
                tempRecomendacao.setRefeicaoCodigo(rs.getInt("refeicaocodigo"));
                tempRecomendacao.setRefeicaoDescricao(rs.getString("refeicaodescricao"));
                resultList.add(tempRecomendacao);
            }
            dao.close();
            List<Integer> tipoCodigoList = new ArrayList<Integer>();
            for (TempRecomendacao t : resultList) {
                if (! tipoCodigoList.contains(t.getTipoCodigo())) {
                    tipoCodigoList.add(t.getTipoCodigo());
                }
            }
            xml += "<result>";
            for (Integer tipoCodigo : tipoCodigoList) {
                xml += "<recomendacao>";
                xml += "<tipocodigo>" + tipoCodigo + "</tipocodigo>";
                List<String> refeicaoList = new ArrayList<String>();
                for (TempRecomendacao t : resultList) {
                    if (t.getTipoCodigo() == tipoCodigo && ! refeicaoList.contains(String.valueOf(t.getRefeicaoCodigo()))) {
                        refeicaoList.add(String.valueOf(t.getRefeicaoCodigo()));
                    }
                }
                for (String r : refeicaoList) {
                    xml += "<refeicao>";
                    xml += "<refeicaocodigo>" + r + "</refeicaocodigo>";
                    for (TempRecomendacao t : resultList) {
                        if (t.getTipoCodigo() == tipoCodigo && t.getRefeicaoCodigo() == Integer.parseInt(r)) {
                            xml += "<refeicaodescricao>" + t.getRefeicaoDescricao() + "</refeicaodescricao>";
                            break;
                        }
                    }
                    xml += "<itens>";
                    for (TempRecomendacao t : resultList) {
                        if (t.getTipoCodigo() == tipoCodigo && t.getRefeicaoCodigo() == Integer.parseInt(r)) {
                            xml += "<item>";
                                xml += "<codigo>" + t.getAlimentoCodigo() + "</codigo>";
                                xml += "<descricao>" + t.getAlimentoDescricao() + "</descricao>";
                            xml += "</item>";
                        }
                    }
                    xml += "</itens>";
                    xml += "</refeicao>";
                }
                xml += "</recomendacao>";
            }
        } catch (SQLException ex) {

        }
        xml += "</result>";
        PrintWriter out = response.getWriter();
        try {
            out.println(xml);
        } finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
